#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
PLAY_TIMES = 10  # 游戏次数
NUM_LEN = 4  	 # 数字位数
IS_LOG = False  	 # 是否打印提示

rand_num = []  	# 随机数
times = 0


def log(*args, **kwargs):
	if IS_LOG:
		print(*args, **kwargs)


# 游戏初始化，重置数据，生成新的随机数
def init():
	global times, rand_num
	times = 0
	rand_num = []
	num_list = list(range(0, 10))
	for i in range(NUM_LEN):
		index = random.randint(0, len(num_list) -1)
		rand_num.append(num_list[index])
		num_list.pop(index)
		log(index, num_list)
	log(rand_num)


# 检验是否正确
def check(num):
	A = 0
	B = 0
	for i in range(len(num)):
		n = int(num[i])
		try:
			index = rand_num.index(n)
		except ValueError:
			continue
		if i == index:
			A += 1
		else:
			B += 1
	print('A%dB%d' % (A, B))
	return A == NUM_LEN


# 获胜
def win():
	print('你猜对啦！')


# 失败
def lose():
	print('笨蛋~~~，游戏失败！')


# 检验输入是否正确
def examine(num):
	if len(num) != NUM_LEN:
		print('请输入%d位不重复数字' % NUM_LEN)
		return False
	return True


# 开始游戏
def play():
	global times
	# 初始化
	init()
	# 游戏主流程
	while times < PLAY_TIMES:
		# 获取输入的数字
		num = input('第%d次：' % (times+1))
		# 检验输入格式是否正确
		if not examine(num):
			continue
		# 检验数字是否正确
		if check(num):
			win()
			break
		else:
			times += 1
	else:
		lose()


play()
